package com.longrise.co.ui.kckp;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.longrise.co.R;

import java.util.List;



/**
 * 百度地图检索关键字结果
 * Created by Administrator on 2017/4/19.
 */
public class KeyResultAdapter extends BaseAdapter {

    private Context mContext;

    private List<PoiInfo> infoLists;

    private PoiSearch search;

    private PoiCitySearchOption option;

    public KeyResultAdapter(Context context) {
        this.mContext = context;
        initData();
    }

    public void setCityName(String cityName) {
        if (TextUtils.isEmpty(cityName)) {
            cityName = "";
        }
        if (null != option) {
            option.city(cityName);
        }
    }

    public List<PoiInfo> getInfoLists() {
        return infoLists;
    }

    public void setInfoLists(List<PoiInfo> infoLists) {
        this.infoLists = infoLists;
        notifyDataSetChanged();
    }

    private void initData() {
        if (search == null) {
            search = PoiSearch.newInstance();
            search.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
                @Override
                public void onGetPoiResult(PoiResult res) {
                    //获取POI检索结果
                    if (res == null || res.getAllPoi() == null) {
                        //未找到相关结果
                        Toast.makeText(mContext, "检索位置失败，请检查定位权限和网络连接", Toast.LENGTH_LONG).show();
                        return;
                    }
                    //数据源发生改变刷新界面的显示
                    infoLists = res.getAllPoi();
                    notifyDataSetChanged();
                }

                @Override
                public void onGetPoiDetailResult(PoiDetailResult result) {
                    //获取Place详情页检索结果

                }
            });
        }
        if (option == null) {
            option = new PoiCitySearchOption().city("");
        }
    }

    public void refresh(CharSequence prefix) {
        String prefixString = prefix.toString().toLowerCase();
        search.searchInCity(option.keyword(prefixString));
    }

    @Override
    public int getCount() {
        return (infoLists == null) ? 0 : infoLists.size();
    }

    @Override
    public Object getItem(int position) {
        return infoLists.get(position).name;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_keyresult_item, null);
            holder.nameTV = (TextView) convertView.findViewById(R.id.keyresult_tv_name);
            holder.addTV = (TextView) convertView.findViewById(R.id.keyresult_tv_address);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nameTV.setText(infoLists.get(position).name);
        holder.addTV.setText(infoLists.get(position).address);
        return convertView;
    }

    private class ViewHolder {
        public TextView nameTV;
        public TextView addTV;
    }

//    private class KeyFilter extends Filter {
//
//        //内容发生变化时执行
//        @Override
//        protected FilterResults performFiltering(CharSequence prefix) {
//            final FilterResults results = new FilterResults();
//
//            if (prefix != null && prefix.length() > 0) {
//                String prefixString = prefix.toString().toLowerCase();
//                //检索结果有延迟，在回调中做后续处理
//                search.requestSuggestion(option.keyword(prefixString));
//            }
//
//            return results;
//        }
//
//        @Override
//        protected void publishResults(CharSequence constraint, FilterResults results) {
//            if (results.count > 0) {
//                notifyDataSetChanged();
//            } else {
//                notifyDataSetInvalidated();
//            }
//        }
//    }
}